/*
 * @Author: 杜二康 3224811364@qq.com
 * @Date: 2024-07-16 14:41:15
  * @LastEditTime: 2024-07-16 14:42:04
 * @FilePath: src/hooks/useNavPage.ts
 * @Description:
 */
import useGetUserInfo from "./useGetUserInfo";
import {useEffect} from "react";
import {useLocation, useNavigate} from "react-router-dom";
import {isLoginOrRegister, isNoNeedUserInfo, LOGIN_PATHNAME, MANAGE_INDEX_PATHNAME} from "../constants";

function useNavPage(waitingUser: boolean) {

  const {username} = useGetUserInfo()
  const {pathname} = useLocation()
  const nav = useNavigate()
  useEffect(() => {
    if (waitingUser) {
      return
    }
    if (username) {
      //   登录了
      if (isLoginOrRegister(pathname)) {
        nav(MANAGE_INDEX_PATHNAME)
        return;
      }
    } else if (isNoNeedUserInfo(pathname)) {
      //   没有登录
      return;
    } else {
      nav(LOGIN_PATHNAME)
    }

  }, [username, pathname]);
}

export default useNavPage
